package com.cheyun.netsalev3.view

import android.content.Intent
import android.util.Log
import android.view.View
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProviders
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.adapter.ImageGridAdapter
import com.cheyun.netsalev3.adapter.SecondAdpetr
import com.cheyun.netsalev3.bean.ClueDetails
import com.cheyun.netsalev3.bean.records.Follow
import com.cheyun.netsalev3.databinding.ActivityClueDetailMingXiBinding
import com.cheyun.netsalev3.utils.BaseActivity
import com.cheyun.netsalev3.utils.InjectorUtils
import com.cheyun.netsalev3.viewmodel.ClueDetailMingXiActivityViewModel
import com.jeremyliao.liveeventbus.LiveEventBus
import java.io.Serializable


class ClueDetailMingXiActivity : BaseActivity<ActivityClueDetailMingXiBinding>() {
    override fun initContentView(): Int {
        return R.layout.activity_clue_detail_ming_xi
    }
    var data:ClueDetails? = null

    override fun initViewModel(): ViewModel? {
        data = intent.getSerializableExtra("data") as ClueDetails
        var follow = intent.getSerializableExtra("follow")  as ArrayList<Follow>
        var face = InjectorUtils.getClueDetailMingXiActivityViewModelFactory(data!!,follow)
        return ViewModelProviders.of(this, face).get(ClueDetailMingXiActivityViewModel::class.java)
    }


    lateinit var adpter :SecondAdpetr
    private var imageAdpater: ImageGridAdapter? = null

    override fun initView() {
        adpter = SecondAdpetr(R.layout.clue_adapter_group_item,R.layout.clue_item_adapter_item,binding.viewModel)
        binding.recyclerView.adapter = adpter
//        binding.headview.rightBut?.visibility = View.GONE;
        binding.headview.rightBut?.setOnClickListener {
            var intent = Intent(this, EditClubActivity::class.java)
            intent.putExtra("data", binding.viewModel?.data as Serializable)
            intent.putExtra("follow", binding.viewModel?.follow as Serializable)
            startActivity(intent)
        }
        LiveEventBus.get()
                .with("clueupdatgemingxi", ClueDetails::class.java)
                .observe(this, Observer {
                    Log.d("mingxi","change")
                    binding.viewModel?.refreshClueDetails(it)
                    if(imageAdpater!=null){
                        if(it?.state.equals("5") || it?.state.equals("10")) {
                            imageAdpater?.replaceAll(it?.cert_image_url)
                        }
                    }
                })


        if(data?.state.equals("5") || data?.state.equals("10")){
            binding.imageLayout.visibility = View.VISIBLE;
            imageAdpater = ImageGridAdapter(this);
            binding.imageGrid.adapter = imageAdpater
            imageAdpater?.replaceAll(data?.cert_image_url)
        }
        binding.imageLayout.setOnClickListener{
            binding.imageGrid.visibility = if(binding.imageGrid.visibility == View.VISIBLE) View.GONE else View.VISIBLE
        }
        if(!data?.vin.isNullOrEmpty() || data?.Level.equals("O")){
            binding.headview.rightBut?.visibility = View.GONE
            binding.imageLayout.visibility = View.GONE
        }

    }

    override fun initData() {
        binding.viewModel?.listdata?.observe(this, Observer { list ->
            adpter.setData(list)
        })
    }

}
